package cn.iocoder.yudao.module.promotion.controller.admin.domainmanage;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.module.promotion.controller.admin.domainmanage.vo.*;
import cn.iocoder.yudao.module.promotion.convert.domainmanage.DomainManageConvert;
import cn.iocoder.yudao.module.promotion.dal.dataobject.domainmanage.DomainManageDO;
import cn.iocoder.yudao.module.promotion.service.domainmanage.DomainManageService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

@Tag(name = "管理后台 - 域名管理")
@RestController
@RequestMapping("/promotion/domain-manage")
@Validated
public class DomainManageController {

    @Resource
    private DomainManageService domainManageService;

    @PostMapping("/create")
    @Operation(summary = "创建域名管理")
    @PreAuthorize("@ss.hasPermission('zfkj:domain-manage:create')")
    public CommonResult<Long> createDomainManage(@Valid @RequestBody DomainManageCreateReqVO createReqVO) {
        return success(domainManageService.createDomainManage(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新域名管理")
    @PreAuthorize("@ss.hasPermission('zfkj:domain-manage:update')")
    public CommonResult<Boolean> updateDomainManage(@Valid @RequestBody DomainManageUpdateReqVO updateReqVO) {
        domainManageService.updateDomainManage(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除域名管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('zfkj:domain-manage:delete')")
    public CommonResult<Boolean> deleteDomainManage(@RequestParam("id") Long id) {
        domainManageService.deleteDomainManage(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得域名管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('zfkj:domain-manage:query')")
    public CommonResult<DomainManageRespVO> getDomainManage(@RequestParam("id") Long id) {
        DomainManageDO domainManage = domainManageService.getDomainManage(id);
        return success(DomainManageConvert.INSTANCE.convert(domainManage));
    }

    @GetMapping("/list")
    @Operation(summary = "获得域名管理列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('zfkj:domain-manage:query')")
    public CommonResult<List<DomainManageRespVO>> getDomainManageList(@RequestParam("ids") Collection<Long> ids) {
        List<DomainManageDO> list = domainManageService.getDomainManageList(ids);
        return success(DomainManageConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得域名管理分页")
    @PreAuthorize("@ss.hasPermission('zfkj:domain-manage:query')")
    public CommonResult<PageResult<DomainManageRespVO>> getDomainManagePage(@Valid DomainManagePageReqVO pageVO) {
        PageResult<DomainManageDO> pageResult = domainManageService.getDomainManagePage(pageVO);
        return success(DomainManageConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/domainList")
    @Operation(summary = "获得域名管理分页")
    @PreAuthorize("@ss.hasPermission('zfkj:domain-manage:query')")
    public CommonResult<List<DomainManageRespVO>> domainList(@Valid DomainManagePageReqVO pageVO) {
        List<DomainManageDO> pageResult = domainManageService.domainList(pageVO);
        return success(DomainManageConvert.INSTANCE.convertList(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出域名管理 Excel")
    @PreAuthorize("@ss.hasPermission('zfkj:domain-manage:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDomainManageExcel(@Valid DomainManageExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<DomainManageDO> list = domainManageService.getDomainManageList(exportReqVO);
        // 导出 Excel
        List<DomainManageExcelVO> datas = DomainManageConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "域名管理.xls", "数据", DomainManageExcelVO.class, datas);
    }

    @PutMapping("/close")
    @Operation(summary = "关闭域名")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('zfkj:domain-manage:close')")
    public CommonResult<Boolean> closeDomainManage(@RequestParam("id") Long id) {
        domainManageService.closeDomainManage(id);
        return success(true);
    }
}
